package com.develop.mybluetooth.adapter

import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattService
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.develop.mybluetooth.R
import com.develop.mybluetooth.databinding.ItemBleServiceBinding
import androidx.core.view.isVisible


class ServicesAdapter : BaseQuickAdapter<BluetoothGattService, ServicesAdapter.VH>() {

    class VH(
        parent: ViewGroup,
        val binding: ItemBleServiceBinding = ItemBleServiceBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)
    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {

        return VH(parent)
    }



    override fun onBindViewHolder(holder: VH, position: Int, item: BluetoothGattService?) {
        holder.binding.tvServiceTitle.text = context.getString(R.string.s_service) + "（"+position + ")"
        holder.binding.tvUuid.text = item?.uuid.toString()
        holder.binding.tvType.text = item?.type.toString()

        holder.binding.rvCharacteristics.layoutManager = LinearLayoutManager(context)
        val cAdapter = CharacteristicsAdapter()
        holder.binding.rvCharacteristics.adapter = cAdapter
        val characteristic = item?.characteristics
        cAdapter.submitList(characteristic)
        holder.binding.llBleServiceItem.setOnClickListener {
            if(holder.binding.rvCharacteristics.isVisible){
                holder.binding.rvCharacteristics.visibility = View.GONE
            }else{
                holder.binding.rvCharacteristics.visibility = View.VISIBLE
            }
        }
    }

}